package util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtil {
	private static ObjectMapper mapper = new ObjectMapper();

	public static String objetc2Json(Object obj) throws JsonProcessingException {
		return mapper.writeValueAsString(obj);
	}

	public static Object json2Object(String json, Class clz)
			throws JsonParseException, JsonMappingException, IOException {
		return mapper.readValue(json, clz);
	}

	public static Map<String, Integer> json2Map(String s) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		String a = s.replace("{", "").replace("}", "").replaceAll("\"", "");
		String b[] = a.split(",");
		for (int i = 0; i < b.length; i++) {
			String key = b[i].split(":")[0];
			int value = Integer.parseInt(b[i].split(":")[1]);
			map.put(key, value);
		}
		return map;
	}
}
